/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.enterprise.model.FiscalPeriodVariant;
import org.crystalrange.enterprise.service.FiscalPeriodVariantService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class FiscalPeriodVariantListController extends BaseController {

    protected FiscalPeriodVariantService fiscalPeriodVariantService;
    protected FiscalPeriodVariant selectedFiscalPeriodVariant;
    protected Listbox listboxFiscalPeriodVariant;
    protected Search searchFiscalPeriodVariant;
    protected List<FiscalPeriodVariant> fiscalPeriodVariants;

    public FiscalPeriodVariantService getFiscalPeriodVariantService() {
        return this.fiscalPeriodVariantService;
    }

    public void setFiscalPeriodVariantService(FiscalPeriodVariantService fiscalPeriodVariantService) {
        this.fiscalPeriodVariantService = fiscalPeriodVariantService;
    }

    public FiscalPeriodVariant getSelectedFiscalPeriodVariant() {
        return this.selectedFiscalPeriodVariant;
    }

    public void setSelectedFiscalPeriodVariant(FiscalPeriodVariant selectedFiscalPeriodVariant) {
        this.selectedFiscalPeriodVariant = selectedFiscalPeriodVariant;
    }

    public List<FiscalPeriodVariant> getFiscalPeriodVariants() {
        return this.fiscalPeriodVariants;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchFiscalPeriodVariant = new Search(FiscalPeriodVariant.class);
        this.searchFiscalPeriodVariant.setMaxResults(100);
        this.searchFiscalPeriodVariant.addFetch("createdBy");
        this.searchFiscalPeriodVariant.addFetch("updatedBy");
        this.fiscalPeriodVariants = this.fiscalPeriodVariantService.search(this.searchFiscalPeriodVariant);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemFiscalPeriodVariantCreate(Event event) throws Exception {
        this.fiscalPeriodVariantCreate();
    }

    public void onClick$menuitemFiscalPeriodVariantEdit(Event event) throws Exception {
        this.fiscalPeriodVariantEdit();
    }

    public void onClick$menuitemFiscalPeriodVariantDelete(Event event) throws Exception {
        this.fiscalPeriodVariantDelete();
    }

    public void onClick$menuitemFiscalPeriodVariantSearch(Event event) throws Exception {
        this.fiscalPeriodVariantSearch();
    }

    public void onClick$menuitemFiscalPeriodVariantRefresh(Event event) throws Exception {
        this.fiscalPeriodVariantRefresh();
    }

    public void onClick$toolbarbuttonFiscalPeriodVariantCreate(Event event) throws Exception {
        this.fiscalPeriodVariantCreate();
    }

    public void onClick$toolbarbuttonFiscalPeriodVariantEdit(Event event) throws Exception {
        this.fiscalPeriodVariantEdit();
    }

    public void onClick$toolbarbuttonFiscalPeriodVariantDelete(Event event) throws Exception {
        this.fiscalPeriodVariantDelete();
    }

    public void onClick$toolbarbuttonFiscalPeriodVariantSearch(Event event) throws Exception {
        this.fiscalPeriodVariantSearch();
    }

    public void onClick$toolbarbuttonFiscalPeriodVariantRefresh(Event event) throws Exception {
        this.fiscalPeriodVariantRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.fiscalPeriodVariantEdit();
    }
    
    private void fiscalPeriodVariantCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/fiscalPeriodVariant.zul");
    }

    private void fiscalPeriodVariantEdit() throws Exception {
        if (this.selectedFiscalPeriodVariant != null) {
            this.execution.sendRedirect("~./zul/html/fiscalPeriodVariant.zul?Id=" + this.selectedFiscalPeriodVariant.getId().toString());
        }
    }

    private void fiscalPeriodVariantDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxFiscalPeriodVariant.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    FiscalPeriodVariant fiscalPeriodVariant = (FiscalPeriodVariant) selectedItem.getValue();
                    this.fiscalPeriodVariantService.remove(fiscalPeriodVariant);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.fiscalPeriodVariants = this.fiscalPeriodVariantService.search(this.searchFiscalPeriodVariant);
        this.binder.loadAll();
    }

    private void fiscalPeriodVariantSearch() throws Exception {
        this.execution.setAttribute("searchFiscalPeriodVariant", this.searchFiscalPeriodVariant);

        Window window = (Window) this.execution.createComponents("~./zul/html/fiscalPeriodVariantSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchFiscalPeriodVariant = (Search) this.execution.getAttribute("searchFiscalPeriodVariant");
        }

        this.fiscalPeriodVariants = this.fiscalPeriodVariantService.search(this.searchFiscalPeriodVariant);
        this.binder.loadAll();
    }

    private void fiscalPeriodVariantRefresh() {
        this.fiscalPeriodVariants = this.fiscalPeriodVariantService.search(this.searchFiscalPeriodVariant);
        this.binder.loadAll();
    }
}
